package com.ruoyi.system.domain.excelImport;


import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.mstnExcel.ExcelImport;
import com.ruoyi.system.domain.MstnCatalysts;
import com.ruoyi.system.domain.MstnCatalystsCharts;
import lombok.Data;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Data
public class MstnCatalystsImport {

    private int rowNum;

    /** 催化剂名称 */
    @ExcelImport("催化剂名称")
    private String catalystsName;

    /** 催化剂类型号 */
    @ExcelImport("催化剂类型号")
    private String categoryName;

    /** 模块型号 */
    @ExcelImport("模块型号")
    private String moduleName;

    /** 模块l */
    @ExcelImport("模块l")
    private BigDecimal moduleL;

    /** 模块w */
    @ExcelImport("模块w")
    private BigDecimal moduleW;

    /** 模块h */
    @ExcelImport("模块h")
    private BigDecimal moduleH;

    /** 每层催化剂净高度 */
    @ExcelImport("每层催化剂净高度")
    private BigDecimal catalystsH;

    /** 每个模块毛重 */
    @ExcelImport("每个模块毛重")
    private BigDecimal moduleKg;

    /** 误差率 */
    @ExcelImport("误差")
    private BigDecimal errorRate;

    /** 波纹宽度 */
    @ExcelImport("波纹宽度")
    private BigDecimal rippleW;

    /** 波纹高度 */
    @ExcelImport("波纹高度")
    private BigDecimal rippleH;

    /** 波纹板厚度 */
    @ExcelImport("波纹板厚度")
    private BigDecimal rippleThickness;

    /** 平板厚度 */
    @ExcelImport("平板厚度")
    private BigDecimal plateThickness;

    /** 节距 */
    @ExcelImport("节距")
    private BigDecimal pitch;

    /** 几何比表面积 */
    @ExcelImport("几何比表面积")
    private BigDecimal geometryAreaRate;

    /** 开孔率 */
    @ExcelImport("开孔率")
    private BigDecimal trepanRate;

    /** 磨损率 */
    @ExcelImport("磨损率")
    private BigDecimal wearRate;

//    1=脱硝效率、2=活性数据、3=SO2氧化铝、4=氨逃逸、5=压力降
    @ExcelImport("脱销效率x")
    private String efficiencyX;
    @ExcelImport("活性数据x")
    private String activeX;
    @ExcelImport("SO2氧化铝x")
    private String so2X;
    @ExcelImport("氨逃逸x")
    private String amoniaX;
    @ExcelImport("压力降x")
    private String pressureX;
    @ExcelImport("脱销效率y")
    private String efficiencyY;
    @ExcelImport("活性数据y")
    private String activeY;
    @ExcelImport("SO2氧化铝y")
    private String so2Y;
    @ExcelImport("氨逃逸y")
    private String amoniaY;
    @ExcelImport("压力降y")
    private String pressureY;

    public MstnCatalysts toMstnCatalysts() {
        MstnCatalysts mstnCatalysts = new MstnCatalysts();
        BeanUtils.copyBeanProp(mstnCatalysts, this);
        List<MstnCatalystsCharts> list = new ArrayList<>();
        if (this.efficiencyX != null && this.efficiencyY != null) {
            String[] efficiencyX = this.efficiencyX.split(",");
            String[] efficiencyY = this.efficiencyY.split(",");
            if (efficiencyX.length != efficiencyY.length){
                throw new RuntimeException("第" + this.rowNum + "行:催化剂【" + this.catalystsName + "】脱销效率数据格式错误");
            }
            insertList(list, "1", efficiencyX, efficiencyY);
        }
        if (this.activeX != null && this.activeY != null) {
            String[] activeX = this.activeX.split(",");
            String[] activeY = this.activeY.split(",");
            if (activeX.length != activeY.length){
                throw new RuntimeException("第" + this.rowNum + "行:催化剂【" + this.catalystsName + "】活性数据数据格式错误");
            }
            insertList(list, "2", activeX, activeY);
        }
        if (this.so2X != null && this.so2Y != null) {
            String[] so2X = this.so2X.split(",");
            String[] so2Y = this.so2Y.split(",");
            if (so2X.length != so2Y.length){
                throw new RuntimeException("第" + this.rowNum + "行:催化剂【" + this.catalystsName + "】SO2氧化铝数据格式错误");
            }
            insertList(list, "3", so2X, so2Y);
        }
        if (this.amoniaX != null && this.amoniaY != null) {
            String[] amoniaX = this.amoniaX.split(",");
            String[] amoniaY = this.amoniaY.split(",");
            if (amoniaX.length != amoniaY.length){
                throw new RuntimeException("第" + this.rowNum + "行:催化剂【" + this.catalystsName + "】氨逃逸数据格式错误");
            }
            insertList(list, "4", amoniaX, amoniaY);
        }
        if (this.pressureX != null && this.pressureY != null) {
            String[] pressureX = this.pressureX.split(",");
            String[] pressureY = this.pressureY.split(",");
            if (pressureX.length != pressureY.length){
                throw new RuntimeException("第" + this.rowNum + "行:催化剂【" + this.catalystsName + "】压力降数据格式错误");
            }
            insertList(list, "5", pressureX, pressureY);
        }
        mstnCatalysts.setMstnCatalystsChartsList(list);
        return mstnCatalysts;
    }

    private void insertList(List<MstnCatalystsCharts> list, String type,String[] x, String[]y){
        if (x.length < 2){
            return;
        }
        BigDecimal startX = new BigDecimal(x[0]);
        BigDecimal startY = new BigDecimal(y[0]);


        for (int i = 1; i < x.length; i++){
            MstnCatalystsCharts pressure = new MstnCatalystsCharts();
            pressure.setChartsType(type);
            pressure.setXValue(new BigDecimal(x[i]));
            pressure.setYValue(new BigDecimal(y[i]));
            pressure.setStartX(startX);
            pressure.setEndX(new BigDecimal(x[i]));
            pressure.setOffsetValue(pressure.getYValue());
            pressure.setRate(startY.subtract(pressure.getYValue()).divide(pressure.getEndX().subtract(startX), 8, BigDecimal.ROUND_HALF_UP));
            list.add(pressure);
            startX = pressure.getXValue();
            startY = pressure.getYValue();
        }
    }
}
